﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Windows.Forms;

namespace BLL
{
    /// <summary>
    /// 获取Record数据保存的位置，并实现保存、读取、添加、更新、删除的功能
    /// </summary>
    public class RecordManagement
    {
       private RecordCollection recordList = new RecordCollection();

        public RecordManagement()
        {
        }
        /// <summary>
        /// 获取record文件夹的绝对路径(record文件夹中保存着record.xml)
        /// </summary>
        public static string DirOfRecordListFile
        {
            get
            {
                string dir = Application.StartupPath + "\\record";
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                return dir;
            }
        }
        /// <summary>
        /// 获取record.xml文件的绝对路径
        /// </summary>
        public static string PathOfRecordListFile
        {
            get
            {
                string path = DirOfRecordListFile + "\\record.xml";
                if (!File.Exists(path))
                {
                    FileStream fs = null;
                    try
                    {
                        fs = File.Create(path);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    finally
                    {
                        if (fs != null)
                        {
                            fs.Close();
                        }
                    }
                }
                return path;
            }
        }
        /// <summary>
        /// 获取或设置记录列表
        /// </summary>
        public RecordCollection RecordList
        {
            get
            {
                return recordList;
            }
            set
            {
                recordList = value;
            }
        }

        /// <summary>
        /// 将数据保存到record.xml
        /// </summary>
        public bool SaveDataToFile()
        {
            StreamWriter sw = new StreamWriter(RecordManagement.PathOfRecordListFile, false, Encoding.Default);
            XmlSerializer xml = new XmlSerializer(typeof(RecordCollection));
            try
            {
                xml.Serialize(sw, recordList);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return false;
            }
            finally
            {
                if (sw != null)
                {
                    sw.Close();
                }
            }

            return true;
        }

        /// <summary>
        /// 从record.xml中读取数据
        /// </summary>
        public bool LoadDataFromFile()
        {
            StreamReader sr = new StreamReader(RecordManagement.PathOfRecordListFile, Encoding.Default);
            XmlSerializer xml = new XmlSerializer(typeof(RecordCollection));
            try
            {
                recordList = (RecordCollection)xml.Deserialize(sr);
            }
            catch //(Exception ex)
            {
                //MessageBox.Show(ex.ToString());
                return false;
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                }
            }

            return true;
        }

        /// <summary>
        /// 将一条新记录保存入record.xml文件和RecordManagement.AcccountList中
        /// </summary>
        ///<param name="rec" Record类的对象></param>
        public void AddRecord(Record rec)
        {
            this.RecordList.Add(rec);
            this.SaveDataToFile();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="rc"></param>
        /// <param name="bWrite"></param>
        /// <returns>返回各条记录金额之和</returns>
        public void AddRecord(RecordCollection rc,bool bWrite)
        {
           
            if (rc == null)
                return;
            for (int i = 0; i < rc.Count; i++)
            {
                if (recordList.Count == 0)
                    rc[i].ID = 1;
                else
                    rc[i].ID = recordList[recordList.Count - 1].ID + 1;
                this.RecordList.Add(rc[i]);
            }
            if(bWrite)
                this.SaveDataToFile();
        }

        public Record GetLastRecord(int iAccountID)
        {
            if (recordList.Count==0)
            {
                return null;
            }
            for (int i = recordList.Count; i > 0;i-- )
            {
                if (recordList[i-1].AccountID==iAccountID)
                {
                    return recordList[i - 1];
                }
            }
            return null;
        }

        /// <summary>
        /// 将该ID的记录从record.xml文件和RecordManagement.AcccountList中删除
        /// </summary>
        ///<param name="iID" 该账户对应ID></param>
        public void DelRecord(int iID)
        {
            foreach (Record record in this.RecordList)
            {
                if (record.ID == iID)
                {
                    this.RecordList.Remove(record);
                    break;     //每次删除操作都会影响RecordList，导致无法继续foreach循环，因此每次只能执行一次删除操作
                }
            }
            this.SaveDataToFile();
        }

        /// <summary>
        /// 将该ID的记录在record.xml文件和RecordManagement.RecordList中更新
        /// </summary>
        ///<param name="rec" Record类的对象></param>
        public void UpdateRecord(Record rec)
        {
            int icount = 0, iwhere = 0;
            foreach (Record record in this.RecordList)
            {
                if (record.ID == rec.ID)
                {
                    iwhere = icount;
                    this.RecordList[iwhere].ID = rec.ID;
                    this.RecordList[iwhere].Amount = rec.Amount;
                    this.RecordList[iwhere].AccountID = rec.AccountID;
                    this.RecordList[iwhere].TypeID = rec.TypeID;
                    this.RecordList[iwhere].Remark = rec.Remark;
                    this.RecordList[iwhere].Time = rec.Time;
                    this.SaveDataToFile();
                    break;     //每次删除操作都会影响RecordList，导致无法继续foreach循环，因此每次只能执行一次删除操作
                }
                icount++;
            }
        }

        //获取该ID对应的record类的对象
        public Record GetRecord(int iID)
        {
            for (int i = 0; i < RecordList.Count; i++)
            {
                if (RecordList[i].ID == iID)
                {
                    return RecordList[i];
                }
            }
            return null;
        }

    }
}
